import BaseController from '../../core/controllers/Base.controller';
import storageApi from '../../core/storage/storage';

@Inject('$log', '$scope', '$state', '$rootScope', 'patrolService', 'ngDialogService', '$compile')
class PatrolGISController extends BaseController {
  constructor() {
    super();

    this.resource = {
      map: null,
      data: {
        selectedItem: null, // 当前选择的巡查人员
        patrolTime: null,
      },
    };

    this.initWorker();

    this.initMap();
  }

  /*eslint-disable */
  initMap() {
    this.resource.map = new BMap.Map("allmap");
    this.resource.map.enableScrollWheelZoom(true);

    var point = new BMap.Point(120.785739,32.462499);
    this.resource.map.centerAndZoom(point, 15);
  }

  initWorker() {
    this.$scope.$watch('vm.resource.data.selectedItem', (newValue) => {
      if (newValue) {
        this.queryGIS(newValue);
      }
    });
  }

  addMarker(patrol, index) {

    const status = patrol.recordStatus ==3 ? 1 : 2;

    const icon = new BMap.Icon(require('../../assets/images/patrol/markers.png'), new BMap.Size(23, 24), {
      offset: new BMap.Size(15, 20),
      imageOffset: new BMap.Size(0-(status-1)*46, 1 - index * 25),
    });

    const icon_over = new BMap.Icon(require('../../assets/images/patrol/markers.png'), new BMap.Size(23, 24), {
      offset: new BMap.Size(15, 20),
      imageOffset: new BMap.Size(0-2*46, 1 - index * 25),
    });

    const icon_click = new BMap.Icon(require('../../assets/images/patrol/markers.png'), new BMap.Size(25, 30), {
      offset: new BMap.Size(15, 20),
      imageOffset: new BMap.Size(1-3*48, 1 - index * 30),
    });

    const point = new BMap.Point(patrol.batLng, patrol.batLat);
    const marker = new BMap.Marker(point, {icon: icon});

    const opts = {
      width : 200,     // 信息窗口宽度
      height: 110,     // 信息窗口高度
    }

    this.$scope.patrolList.push({
      id: patrol.id,
      statusName: patrol.recordStatus ==1 ? '待整改' : (patrol.recordStatus ==2 ? '待审核' : '已完成'),
      checkNo: patrol.checkNo,
      createdTime: patrol.createdTime,
    });

    let content = '<div class="map-content">';
     content += '<div style="padding:5px 0">状态：{{patrolList['+index+'].statusName}}</div>';
     content += '<div style="padding:5px 0">编号：{{patrolList['+index+'].checkNo}}</div>';
     content += '<div style="padding:5px 0">时间：{{patrolList['+index+'].createdTime | date:"yyyy-MM-dd HH:mm"}}</div>';
     content += '<div style="padding:5px 0;"><a style="color:#0b8bd6" ui-sref="patrolDetail({id: patrolList['+index+'].id})" href="javascript:;">巡查详情</a></div>';
     content += '</div>';

    let infoWindow = new BMap.InfoWindow(content, opts);  // 创建信息窗口对象

    marker.addEventListener("click", () => {
      marker.setIcon(icon_click);
      this.resource.map.openInfoWindow(infoWindow,point); //开启信息窗口
      this.$compile($('.patrol-gis-map').contents())(this.$scope);
      this.$scope.$apply();
    });

    infoWindow.addEventListener("close", () => {
      marker.setIcon(icon);
    });

    infoWindow.addEventListener("open", () => {
      marker.setIcon(icon_click);
    });

    marker.addEventListener("mouseover", (event) => {
      if (!infoWindow.isOpen()) {
        marker.setIcon(icon_over);
      }
    });

    marker.addEventListener("mouseout", () => {
      if (!infoWindow.isOpen()) {
        marker.setIcon(icon);
      }
    });

    this.resource.map.addOverlay(marker);
  }

  queryGIS(worker) {
    this.resource.map.clearOverlays();
    this.$scope.patrolList = [];
    const param = {
      mapParams: {
        patrolUserId: worker.patrolUserId,
        patrolTime: worker.patrolTime,
      }
    }
    this.patrolService.queryGIS(param).then((res) => {
      if (res.success && res.data && res.data.list.length > 0) {
        let points = [];
        res.data.list.forEach((item, index) => {
          this.addMarker(item, index);
          points.push(new BMap.Point(item.batLng, item.batLat));
        });
        // 获取当前人员寻常定位的中心点和缩放程度
        let viewport = this.resource.map.getViewport(points);
        this.resource.map.centerAndZoom(viewport.center, viewport.zoom);
      }
    });
  }

}

export default PatrolGISController;
